package 第七题;

/**
 * @author A18ccms A18ccms_gmail_com
 * @version V1.0
 * @Title:
 * @Package
 * @Description: (用一句话描述该文件做什么)
 * @date
 */

public class ticketsThread implements Runnable {
    private int tickets=10;
    private int count=0;
    public void sellTicket(){

        synchronized (this){
            if (tickets>0){
            if(Thread.currentThread().getName().equals("黄牛党")&&count!=0){
               return;
            }

                tickets--;

                System.out.println(Thread.currentThread().getName()
                +"抢到一张票,剩余"+tickets+"张票");
                if(Thread.currentThread().getName().equals("黄牛党")){
                    count++;
                }

            }
            else{
                System.out.println("票已卖完！");
                return;
            }
        }
    }
    @Override
    public void run() {
        while (tickets > 0) {
            sellTicket();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
